package plantObjects;

import control.Event;
import control.Eventhandler;
import java.util.LinkedList;

/**
 * represents a production unit
 * @author guru
 */

public class Saw implements TransformingUnit {

    private int time;
    private LinkedList<Slap> currentSlap = new LinkedList<Slap>();
    /**
     * Init a saw with a given production time
     * @param time
     */
    public Saw(int time)
    {
        this.time = time;
    }

    public void put(Slap s)
    {
        // check if saw is in use
        if(!checkSawAvail()) System.err.println("Saw is in use ! Can not put another slap to it");
        else
        {
            // add 3 events to the eventhandler with differing time by 1/3 of time
            for( int i = 1 ; i <= 3; i++)
            {
                currentSlap.add(s);
                Eventhandler.add( new Event(Eventhandler.getTime() + (int)(time*i/3), this));
            }
        }
    }

    public Slap get()
    {
        return currentSlap.poll();
    }

/**
 * check if saw is free to use
 * @return true if it is unused / false if used
 */
    public boolean checkSawAvail()
    {
        return currentSlap.isEmpty();
    }
}
